package com.fiveflavors.suittar.gui.mainpanel.view;

import java.util.List;

import twitter4j.User;
import twitter4j.UserWithStatus;

import com.fiveflavors.suittar.SuittarCommand;
import com.fiveflavors.suittar.gui.utils.Converter;

public class SuittarUsersView {
	
	public static StringBuffer getContents(UserWithStatus user,List<User> users,boolean isFollowing) {

		StringBuffer htmlContents = new StringBuffer();

		htmlContents.append("<HTML>");
		htmlContents.append("<BODY>");

		htmlContents.append("<TABLE ALIGN=\"CENTER\" WIDTH=\"95%\">");
		htmlContents.append("<TR><TD>");

		//View Header		
		htmlContents.append("<IMG SRC=\"" + user.getProfileImageURL().toString() + "\">&nbsp;");
		htmlContents.append("<FONT SIZE=\"+2\">");
		
		String title = "";
		int count = 0;
		if (isFollowing) {
			title = "people " + user.getScreenName() + " follow";
			count = user.getFriendsCount();
		}
		else {
			title = "people who follow " + user.getScreenName();
			count = user.getFollowersCount();
		}
		
		htmlContents.append("<FONT SIZE=\"+2\">" + title + " </FONT>");
		htmlContents.append("</FONT>");
		htmlContents.append("<FONT SIZE=\"+1\">");
		htmlContents.append("&nbsp;(" + count + ")");
		htmlContents.append("</FONT>");
		htmlContents.append("<BR>");

		htmlContents.append("</TD></TR>");
		htmlContents.append("<TR><TD>");
		htmlContents.append("<HR align=\"CENTER\" size=\"1\" width=\"100%\">");
		htmlContents.append("</TD></TR>");
		htmlContents.append("</TABLE>");

		//Main Contents(Header) - <TABLE>Tag
		htmlContents.append("<TABLE WIDTH=\"100%\" CELLSPACING=\"10\">");

		//Main Contents (Row) - <TR><TD></TD></TR>
		int colCnt = 0;
		StringBuffer imgHtml = new StringBuffer();
		StringBuffer txtHtml = new StringBuffer();
		
		for(User follower: users) {
			if (colCnt >= 5) {
				htmlContents.append("<TR>");
				htmlContents.append(imgHtml);
				htmlContents.append("</TR>");
				htmlContents.append("<TR>");
				htmlContents.append(txtHtml);
				htmlContents.append("</TR>");
				imgHtml = new StringBuffer();
				txtHtml = new StringBuffer();
				colCnt = 0;
			}
			imgHtml.append(getFollowersRowImageContents(follower));
			txtHtml.append(getUsersRowTextContents(follower));
			colCnt++;
		}
		
		if (imgHtml.length() > 0) {
			for (int i = 0;i < 5 - colCnt; i++) {
				imgHtml.append("<TD></TD>");
				txtHtml.append("<TD></TD>");
			}
			htmlContents.append("<TR>");
			htmlContents.append(imgHtml);
			htmlContents.append("</TR>");
			htmlContents.append("<TR>");
			htmlContents.append(txtHtml);
			htmlContents.append("</TR>");
		}
		
		//Main Contents(Closer) - </TABLE>Tag
		htmlContents.append("</TABLE>");
		
		if (count > users.size()) {
			htmlContents.append("<DIV ALIGN=\"RIGHT\">more...</DIV>");
		}
		htmlContents.append("</BODY>");
		htmlContents.append("</HTML>");
		
		return htmlContents;
	}

	public static StringBuffer getFollowersRowImageContents(User user) {
		StringBuffer buf = new StringBuffer();
		buf.append("<TD valign=\"top\" ALIGN=\"CENTER\">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + user.getId() + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(user.getProfileImageURL()));
		buf.append("</A>");			
		buf.append("</TD>");					
		return buf;
	}
	
	public static StringBuffer getUsersRowTextContents(User user) {
		StringBuffer buf = new StringBuffer();
		buf.append("<TD ALIGN=\"CENTER\">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + user.getId() + "\">");
		buf.append(user.getScreenName());
		buf.append("</A>");
		buf.append("</TD>");		
		return buf;
	}
	
}
